package com.lcf.system.service;

import cn.hutool.core.util.RandomUtil;
import com.lcf.email.service.EmailUtils;
import com.lcf.redis.enums.RedisCachePrefixEnum;
import com.lcf.redis.utils.RedisCacheUtils;
import com.lcf.system.dto.email.EmailDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
 * @author lcf
 */
@Service
public class EmailService {
    @Resource
    private EmailUtils emailUtils;
    @Resource
    private RedisCacheUtils redisCacheUtils;

    public void sendBindEmail(EmailDTO email) {
        String code = RandomUtil.randomNumbers(6);

        // 构建 HTML 内容（验证码标红）
        String htmlContent = String.format("""
                <!DOCTYPE html>
                <html>
                <body>
                  <p>您的验证码为：<span style="color: red; font-weight: bold;">%s</span></p>
                  <p>5 分钟之内有效，请尽快使用，切勿泄露给他人。</p>
                </body>
                </html>
                """, code);

        emailUtils.sendHtml("验证码", htmlContent, email.getEmail());

        String key = RedisCachePrefixEnum.EMAIL_BIND_CODE.getKey(code);
        redisCacheUtils.cache(key, email.getEmail(), Duration.ofMinutes(5));
    }
}
